<template>
  <a-layout class="main-layout">
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="logo">
        <img src="../assets/logo.png" alt="Logo" />
        <h1 v-if="!collapsed">血糖小程序后台</h1>
      </div>
      <a-menu
        v-model:selectedKeys="selectedKeys"
        theme="dark"
        mode="inline"
      >
        <a-menu-item key="dashboard" @click="() => $router.push('/dashboard')">
          <dashboard-outlined />
          <span>仪表盘</span>
        </a-menu-item>
        
        <a-sub-menu key="user-management">
          <template #title>
            <span><team-outlined /><span>用户管理</span></span>
          </template>
          <a-menu-item key="admin-users" @click="() => $router.push('/admin-users')">
            <user-outlined />
            <span>管理员用户</span>
          </a-menu-item>
          <a-menu-item key="wx-users" @click="() => $router.push('/wx-users')">
            <wechat-outlined />
            <span>微信用户管理</span>
          </a-menu-item>
        </a-sub-menu>
        
        <a-sub-menu key="blood-sugar-management">
          <template #title>
            <span><medicine-box-outlined /><span>血糖管理</span></span>
          </template>
          <a-menu-item key="blood-sugar" @click="() => $router.push('/blood-sugar')">
            <database-outlined />
            <span>血糖记录管理</span>
          </a-menu-item>
          <a-menu-item key="blood-sugar-stats" @click="() => $router.push('/blood-sugar-stats')">
            <line-chart-outlined />
            <span>血糖统计分析</span>
          </a-menu-item>
        </a-sub-menu>
        
        <a-sub-menu key="diet-management">
          <template #title>
            <span><coffee-outlined /><span>饮食管理</span></span>
          </template>
          <a-menu-item key="diet" @click="() => $router.push('/diet')">
            <appstore-outlined />
            <span>饮食管理主页</span>
          </a-menu-item>
          <a-menu-item key="diet-foods" @click="() => $router.push('/diet/foods')">
            <shopping-outlined />
            <span>食物管理</span>
          </a-menu-item>
          <a-menu-item key="diet-categories" @click="() => $router.push('/diet/categories')">
            <tags-outlined />
            <span>食物分类</span>
          </a-menu-item>
          <a-menu-item key="diet-recipes" @click="() => $router.push('/diet/recipes')">
            <book-outlined />
            <span>食谱管理</span>
          </a-menu-item>
          <a-menu-item key="diet-records" @click="() => $router.push('/diet/records')">
            <calendar-outlined />
            <span>饮食记录</span>
          </a-menu-item>
        </a-sub-menu>
        
        <a-sub-menu key="article-management">
          <template #title>
            <span><read-outlined /><span>文章管理</span></span>
          </template>
          <a-menu-item key="articles" @click="() => $router.push('/articles')">
            <file-outlined />
            <span>文章列表</span>
          </a-menu-item>
          <a-menu-item key="article-categories" @click="() => $router.push('/articles/categories')">
            <tags-outlined />
            <span>文章分类</span>
          </a-menu-item>
        </a-sub-menu>
        
        <a-sub-menu key="community-management">
          <template #title>
            <span><comment-outlined /><span>社区管理</span></span>
          </template>
          <a-menu-item key="community-posts" @click="() => $router.push('/community/posts')">
            <file-text-outlined />
            <span>帖子管理</span>
          </a-menu-item>
          <a-menu-item key="community-comments" @click="() => $router.push('/community/comments')">
            <message-outlined />
            <span>评论管理</span>
          </a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    
    <a-layout>
      <a-layout-header class="header">
        <div class="header-left">
          <menu-unfold-outlined
            v-if="collapsed"
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
          <menu-fold-outlined
            v-else
            class="trigger"
            @click="() => (collapsed = !collapsed)"
          />
          <a-breadcrumb>
            <a-breadcrumb-item v-for="(item, index) in breadcrumbItems" :key="index">
              {{ item }}
            </a-breadcrumb-item>
          </a-breadcrumb>
        </div>
        
        <div class="header-right">
          <a-dropdown>
            <a class="ant-dropdown-link">
              <a-avatar :size="32">{{ username.substring(0, 1).toUpperCase() }}</a-avatar>
              <span class="username">{{ username }}</span>
              <down-outlined />
            </a>
            <template #overlay>
              <a-menu>
                <a-menu-item key="profile" @click="() => $router.push('/profile')">
                  <user-outlined />
                  个人设置
                </a-menu-item>
                <a-menu-divider />
                <a-menu-item key="logout" @click="handleLogout">
                  <logout-outlined />
                  退出登录
                </a-menu-item>
              </a-menu>
            </template>
          </a-dropdown>
        </div>
      </a-layout-header>
      
      <a-layout-content class="content">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </a-layout-content>
      
      <a-layout-footer class="footer">
        <p>血糖小程序后台管理系统 ©{{ new Date().getFullYear() }}</p>
      </a-layout-footer>
    </a-layout>
  </a-layout>
</template>

<script setup>
import { ref, computed, watch, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '../store/user';
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  DashboardOutlined,
  UserOutlined,
  TeamOutlined,
  MedicineBoxOutlined,
  DatabaseOutlined,
  LineChartOutlined,
  DownOutlined,
  LogoutOutlined,
  WechatOutlined,
  CommentOutlined,
  FileTextOutlined,
  MessageOutlined,
  CoffeeOutlined,
  AppstoreOutlined,
  ShoppingOutlined,
  TagsOutlined,
  BookOutlined,
  CalendarOutlined,
  ReadOutlined,
  FileOutlined
} from '@ant-design/icons-vue';

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();

const collapsed = ref(false);
const selectedKeys = ref([route.name]);

// 计算属性获取用户名
const username = computed(() => userStore.username || 'Admin');

// 面包屑导航
const breadcrumbItems = computed(() => {
  const items = ['首页'];
  if (route.meta.title) {
    items.push(route.meta.title);
  }
  return items;
});

// 监听路由变化，更新菜单选中项
watch(
  () => route.name,
  (newVal) => {
    selectedKeys.value = [newVal];
  }
);

// 退出登录
const handleLogout = () => {
  userStore.logout();
  router.push('/login');
};

// 组件挂载时检查登录状态
onMounted(() => {
  if (!userStore.isLoggedIn) {
    router.push('/login');
  }
});
</script>

<style scoped>
.main-layout {
  min-height: 100vh;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 64px;
  background: #001529;
  padding: 0 16px;
  overflow: hidden;
  color: white;
}

.logo img {
  height: 32px;
  margin-right: 12px;
}

.logo h1 {
  color: white;
  margin: 0;
  font-size: 18px;
  white-space: nowrap;
}

.header {
  background: #fff;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  z-index: 1;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.trigger {
  font-size: 18px;
  line-height: 64px;
  padding: 0 24px;
  cursor: pointer;
  transition: color 0.3s;
}

.trigger:hover {
  color: #1890ff;
}

.username {
  margin: 0 8px;
}

.content {
  margin: 24px 16px;
  padding: 24px;
  background: #fff;
  min-height: 280px;
  border-radius: 4px;
}

.footer {
  text-align: center;
  padding: 12px 50px;
}

/* 页面过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style> 